package com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectPosition

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.firstdemo.network.api.addProblem.AddProblemApi
import com.example.firstdemo.network.api.addProblem.data.PositionDictData
import com.example.firstdemo.network.service.NetworkException
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectPosition.data.SelectPositionUIState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class SelectPositionViewModel : ViewModel() {
    private val _uiState = MutableStateFlow(SelectPositionUIState())
    val uiState = _uiState.asStateFlow()

    var positionList: List<PositionDictData> = emptyList()
    fun getPositionList() {
        viewModelScope.launch {
            try {
                positionList = AddProblemApi.getPosition().data ?: emptyList()
                _uiState.value = _uiState.value.copy(
                    positionList = positionList
                )
            } catch (_: NetworkException) {
            }
        }
    }

    fun filterPosition(str: String) {
        _uiState.value = _uiState.value.copy(
            positionList = positionList.filter { it.label.contains(str) }
        )
    }
}